package com.kingstar.ceres.data;

import com.kingstar.ceres.data.db.CurrencyDBHandler;
import com.kingstar.ceres.data.db.OriginalCsvChannelImpl;
import com.kingstar.ceres.data.db.TaskLockHandler;
import com.kingstar.ceres.data.db.mapper.CustomMapper;
import com.kingstar.ceres.data.db.service.ITaskLockService;
import com.kingstar.ceres.data.db.service.impl.TaskLockServiceImpl;
import com.kingstar.ceres.data.handler.ITaskLockHandler;
import com.kingstar.ceres.data.strategy.csv.AbstractCsvChannel;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ConditionalOnResource(resources = {"classpath:fileMapping.xml"})
@ConditionalOnBean({DataSource.class})
@ConditionalOnClass({MapperScan.class})
@MapperScan(value = {"com.kingstar.ceres.data.db.mapper"})
@AutoConfigureBefore(CsvImportAutoConfigure.class)
public class OperateDbAutoConfigure {

    @Bean
    public ITaskLockService taskLockService(){
        return new TaskLockServiceImpl();
    }

    @Bean
    @ConditionalOnMissingBean
    public ITaskLockHandler taskLockHandler(ITaskLockService taskLockService){
        return new TaskLockHandler(taskLockService);
    }

    @Bean
    @ConditionalOnMissingBean
    public CurrencyDBHandler currencyDBHandler(CustomMapper customMapper){
        return new CurrencyDBHandler(customMapper);
    }

}
